//
//  SecondViewController.m
//  UI11_homework_通讯录
//
//  Created by aaaaaa on 16/6/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "Contant.h"

@interface SecondViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, retain) NSMutableArray<UITextField *> *array;
@property (nonatomic, retain) UIButton *button;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    [self createSubViews];
}

- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveAction:)];
}

- (void)saveAction:(UIBarButtonItem *)bar{
    Contant *contant = [[Contant alloc] init];
    contant.name = self.array[0].text;
    contant.number = self.array[1].text;
    contant.QQ = self.array[2].text;
    
    [self.delegate backContant:contant];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)createSubViews{
    
    self.array = [NSMutableArray array];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.layer.masksToBounds = YES;
    self.button.layer.cornerRadius = 50;
    self.button.frame = CGRectMake(157, 80, 100, 100);
    self.button.layer.borderWidth = 1.0;
    self.button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.button setTitle:@"添加图片" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.button];
    
    NSArray *lableArray = @[@"姓名", @"电话", @"QQ"];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(57, 260, 300, 200)];
    for (int i = 0; i < 3; i++) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10 + 40 * i, view.frame.size.width * 0.3, 30)];
        lable.text = lableArray[i];
        [view addSubview:lable];
        [lable release];
        
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(view.frame.size.width * 0.35, 10 + 40 * i, view.frame.size.width * 0.6, 30)];
        textField.layer.borderWidth = 1.0;
        textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        textField.layer.cornerRadius = 10;
        textField.layer.masksToBounds = YES;
        [view addSubview:textField];
        [textField release];
        
        if (i != 0) {
            textField.keyboardType = UIKeyboardTypeNumberPad;
        }
        
        
        [self.array addObject:textField];
        
    }
    
    [self.view addSubview:view];
    
    
    
    
}

- (void)buttonAction:(UIButton *)button{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{
        
        
    }];
    [picker release];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self.button setImage:image forState:UIControlStateNormal];
    [self.button setTitle:@"" forState:UIControlStateNormal];
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
